using SQLite;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace StockTradTest.Data
{
    //指标数据库结构操作

    public class IndicatorDatabase
    {
        readonly SQLiteAsyncConnection _database;

        public IndicatorDatabase(string dbPath)
        {
            _database = new SQLiteAsyncConnection(dbPath);
            _database.CreateTableAsync<Indicator>().Wait();
        }
        public Task<List<Indicator>> GetIndicatorAsync()
        {
            return _database.Table<Indicator>()
                            .ToListAsync();
        }
        public List<double> GetMa(int n)
        {
            List<Indicator> indicators = GetIndicatorAsync().Result;
            List<double> maX = new List<double>();

            switch(n)
            {
                case 5: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma5); break;
                case 10: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma10); break;
                case 20: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma20); break;
                case 30: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma30); break;
                case 60: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma60); break;
                case 120: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma120); break;
                case 250: foreach (Indicator indicator in indicators) maX.Add(indicator.Ma250); break;
                default:maX = null;break;
            }
            return maX;
        }
        public Task<Indicator> GetIndicatorByDateAsync(int _Date)
        {
            return _database.Table<Indicator>()
                            .Where(i => i.Date == _Date)
                            .FirstOrDefaultAsync();
        }
        public Task<int> SaveIndicatorAsync(Indicator indicator)
        {
            if (indicator.ID != 0)
            {
                return _database.UpdateAsync(indicator);
            }
            else
            {
                return _database.InsertAsync(indicator);
            }
        }
        public async Task<int> SaveAllIndicatorAsync(List<Indicator> indicators)
        {
            List<Indicator> checkIndicators = GetIndicatorAsync().Result;
            if(checkIndicators == null || checkIndicators.Count == 0)
            {
                return await _database.InsertAllAsync(indicators);
            }
            //数据库中最新的日期股票信息
            Indicator upToDateIndicator = checkIndicators[checkIndicators.Count - 1];
            for (int i = 0; i < indicators.Count; i++)
            {
                if (indicators[i].Date == upToDateIndicator.Date)
                {
                    if (indicators[i].Ma5 == upToDateIndicator.Ma5)
                        return await _database.InsertAllAsync(indicators.GetRange(i + 1, indicators.Count - i-1));
                    else
                    {
                        _database.QueryAsync<Indicator>("delete from Indicator").Wait();
                        return await _database.InsertAllAsync(indicators);
                    }
                }
            }
            return await _database.InsertAllAsync(indicators);
        }
    }
 
}
